package com.enjoy.xbase.xui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 描述：基础数据列表适配
 */
public class BaseRvAdapter extends RecyclerView.Adapter<BaseRvViewHolder> {
    private Context context;
    private IDataView dataView;

    private OnItemChildClickListener onItemChildClickListener;
    private OnItemClickListener onItemClickListener;

    public BaseRvAdapter(Context context, IDataView dataView) {
        this.context = context;
        this.dataView = dataView;
    }

    @Override
    public int getItemCount() {
        return dataView.getItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        return dataView.getItemViewType(position);
    }

    @NonNull
    @Override
    public BaseRvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(dataView.getItemLayoutId(viewType), parent, false);
        return new BaseRvViewHolder(itemView, this);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseRvViewHolder holder, int position) {
        dataView.onBindViewHolder(holder, position);
    }

    public OnItemChildClickListener getOnItemChildClickListener() {
        return onItemChildClickListener;
    }

    public void setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
        this.onItemChildClickListener = onItemChildClickListener;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface IDataView {
        int getItemCount();

        int getItemViewType(int position);

        int getItemLayoutId(int viewType);

        void onBindViewHolder(BaseRvViewHolder holder, int position);
    }
    public interface OnItemChildClickListener {
        void onItemChildClick(@NonNull RecyclerView.Adapter adapter, @NonNull View view, int position);
    }
    public interface OnItemClickListener {
        void onItemClick(@NonNull RecyclerView.Adapter adapter, @NonNull View view, int position);
    }
}